package cae;

import org.apache.log4j.Logger;

import cae.gui.CaeClip;
import cae.gui.util.CaeAlert;
import cae.logic.CaeMessageReader;
import cae.logic.CaeRemoteMessageListener;
import cae.logic.file.CaeFileReciver;
import cae.logic.util.CaeClipConfiguration;

import com.jeans.trayicon.WindowsTrayIcon;


public class Launcher {

	private static Logger logger = Logger.getLogger(Launcher.class);
	
	public static void main(String[] args)
	{
		try
		{
			logger.info("Iniciando CaeClip...");
			CaeClip clip = new CaeClip();
			new Thread(new CaeRemoteMessageListener(clip)).start();
			
			CaeMessageReader reader = new CaeMessageReader("textos.txt");
			new Thread(new CaeFileReciver()).start();
			
			logger.info("Iniciado!");
	
			int sleepTime = CaeClipConfiguration.getInstance().getIProp(CaeClipConfiguration.TIME_BETWEEN_MESSAGES);
			
			while(reader.ready() && clip.isShowAutoMessages())
			{
				Thread.sleep(sleepTime);
				
				clip.publishMessage(reader.getMessage(), 1);
			}
		}
		catch(Exception e)
		{
			logger.fatal("Error FATAL ", e);
			WindowsTrayIcon.cleanUp();
			CaeAlert.error("No se pudo iniciar Clip! \nRazon: " + e.getMessage(), null);
			System.exit(-1);
		}

	}
}
